package cl.ccs.migracionbcmc.view.pagebean.usuarios;

import cl.ccs.migracionbcmc.model.Mensaje;

import java.util.Iterator;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import oracle.adf.model.AttributeBinding;
import oracle.adf.model.BindingContext;

import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.component.rich.RichPopup;

import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

public class UsuariosPageBean {
    
    private String mensajeError;
    private String mensajeInfo;
    
    private RichPopup popError;
    private RichPopup popInfo;
    
    private String newPassw2;

    public UsuariosPageBean() {
    }

    /**
     * @return
     */
    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    /**
     * @return
     */
    public String btnModificar_action() {
        
        String passwordOld = (String)getBindingAttributeValue("oldPassw");
        String passwordNew = (String)getBindingAttributeValue("newPassw");
                          
        if (passwordOld.equals(passwordNew)){
            showPopError("Password nueva No puede ser igual a la Actual.");
            return null;
        }else if (!passwordNew.equals(newPassw2)){
            showPopError("El Re-Ingreso de la Password nueva es diferente a la primera");
            return null;
        }
              
        BindingContainer bindings = getBindings();
        OperationBinding operationBinding = bindings.getOperationBinding("modificaPassword");
        Object result = operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return null;
        }
        Mensaje mensaje = (Mensaje)operationBinding.getResult();
        if (mensaje.getCodigoMensaje() != 0){
            showPopError(mensaje.getMensaje());
        }
        else {
            showPopInfo("Password Modificada Exitosamente.");
        }
        
        restoreModifUsuario();
        return null;
    }
    
    private Object getBindingAttributeValue(String bindingVariable) {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        DCBindingContainer binding =
            (DCBindingContainer)app.evaluateExpressionGet(context, "#{bindings}", DCBindingContainer.class);
        AttributeBinding attr = (AttributeBinding)binding.getControlBinding(bindingVariable);
        return attr.getInputValue();

    }
    
    private void restoreModifUsuario() {
        setBindingAttributeValue("oldPassw", "");
        setBindingAttributeValue("newPassw", "");
        setNewPassw2("");
        refreshPartialTarget();
    }
    
    private void refreshPartialTarget() {
        AdfFacesContext.getCurrentInstance().addPartialTarget(findComponentInRoot("txtPasswActual"));
        AdfFacesContext.getCurrentInstance().addPartialTarget(findComponentInRoot("txtNewPassw"));
        AdfFacesContext.getCurrentInstance().addPartialTarget(findComponentInRoot("txtNewPassw2"));
    }
    
    public static UIComponent findComponentInRoot(String id) {
        UIComponent component = null;
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext != null) {
            UIComponent root = facesContext.getViewRoot();
            component = findComponent(root, id);
        }
        return component;
    }

    public static UIComponent findComponent(UIComponent base, String id) {
        if (id.equals(base.getId()))
            return base;

        UIComponent children = null;
        UIComponent result = null;
        Iterator childrens = base.getFacetsAndChildren();
        while (childrens.hasNext() && (result == null)) {
            children = (UIComponent)childrens.next();
            if (id.equals(children.getId())) {
                result = children;
                break;
            }
            result = findComponent(children, id);
            if (result != null) {
                break;
            }
        }
        return result;
    }
    
    private void setBindingAttributeValue(String bindingVariable, Object value) {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        DCBindingContainer binding =
            (DCBindingContainer)app.evaluateExpressionGet(context, "#{bindings}", DCBindingContainer.class);
        AttributeBinding attr = (AttributeBinding)binding.getControlBinding(bindingVariable);
        attr.setInputValue(value);

    }
    
    private void showPopError(String mensaje) {
        setMensajeError(mensaje);
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        getPopError().show(ph);
    }
    
    private void showPopInfo(String mensaje) {
        setMensajeInfo(mensaje);
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        getPopInfo().show(ph);
    }

    public void setMensajeError(String mensajeError) {
        this.mensajeError = mensajeError;
    }

    public String getMensajeError() {
        return mensajeError;
    }

    public void setPopError(RichPopup popError) {
        this.popError = popError;
    }

    public RichPopup getPopError() {
        return popError;
    }

    public void setNewPassw2(String newPassw2) {
        this.newPassw2 = newPassw2;
    }

    public String getNewPassw2() {
        return newPassw2;
    }

    public void setMensajeInfo(String mensajeInfo) {
        this.mensajeInfo = mensajeInfo;
    }

    public String getMensajeInfo() {
        return mensajeInfo;
    }

    public void setPopInfo(RichPopup popInfo) {
        this.popInfo = popInfo;
    }

    public RichPopup getPopInfo() {
        return popInfo;
    }
}
